﻿using System;
using System.Collections.Generic;

public class Dict<Key,Value>
{
    public Dictionary<Key, Value> Dictionary { get; private set; }
    public event Action<Key> OnKeyAdd;
    public event Action<Key> OnKeyRemove;
    public event Action<Key> OnValueChanged;
    public Dict()
    {
        Dictionary = new Dictionary<Key, Value>();
    }
    public void AddOrReplace(Key key,Value value)
    {
        if(IsExist(key))
        {
            if (Dictionary[key].Equals(value))
                return;
            else
            {
                Dictionary[key] = value;
                OnValueChanged(key);
            }
        }
        else
        {
            Dictionary.Add(key, value);
            OnKeyAdd(key);
        }
    }
    public void Remove(Key key)
    {
        if (IsExist(key))
        {
            Dictionary.Remove(key);
            OnKeyRemove(key);
        }
    }
    public bool IsExist(Key key)
    {
        if (Dictionary.ContainsKey(key))
            return true;
        return false;
    }
    public int Count { get { return Dictionary.Count; } }
}
